package com.qa.controller;

import com.qa.common.ServerResponse;
import com.qa.service.IArticleService;
import com.qa.service.ICommentService;
import com.qa.service.IDownloadService;
import com.qa.service.IUserService;
import com.qa.vo.WelcomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author
 * @Date 2020/3/28 22:25
 * @Desc
 **/
@Controller
public class WelcomeController {
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IDownloadService downloadService;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private IUserService userService;

    @GetMapping("/welcome")
    public String welcomePage() {
        return "welcome";
    }

    @GetMapping("/welcome/data")
    @ResponseBody
    public ServerResponse welcomeData() {
        List<Integer> articleCountList = new ArrayList<>();
        List<Integer> downloadCountList = new ArrayList<>();
        List<Integer> commentCountList = new ArrayList<>();
        List<Integer> userCountList = new ArrayList<>();
        List<String> dateList = new ArrayList<>();
        int count = -7;
        int count1 = -6;
        for (int i = 0; i < 7; i++) {
            Date date = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(calendar.DATE, count++);
            date = calendar.getTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String beginTime = format.format(date);

            Date date1 = new Date();
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(date1);
            calendar1.add(calendar1.DATE, count1++);
            date1 = calendar1.getTime();
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");

            String endTime = format1.format(date1);

            dateList.add(endTime);

            int articleCount = articleService.getCount(beginTime, endTime);
            articleCountList.add(articleCount);

            int downloadCount = downloadService.getCount(beginTime, endTime);
            downloadCountList.add(downloadCount);

            int commenetCount = commentService.getCount(beginTime, endTime);
            commentCountList.add(commenetCount);
            int userCount = userService.getCount(beginTime, endTime);
            userCountList.add(userCount);

        }
        WelcomVo vo = new WelcomVo();
        vo.setDate(dateList);
        vo.setArticleCountList(articleCountList);
        vo.setCommentCountList(commentCountList);
        vo.setDownloadCountList(downloadCountList);
        vo.setUserCountList(userCountList);
        return ServerResponse.createBySuccess("成功！", vo);
    }
}